
package util.file.xml;

import java.util.HashMap;
import java.util.Map;

public class BaseDatosXml {
    
    //private List<Tabla> tablas;
    private final Map<String,TablaXml> tablas;

    public BaseDatosXml() {
        //tablas = new ArrayList<Tabla>();
        tablas = new HashMap<String,TablaXml>();
    }

    public void addTabla(TablaXml tabla) {
        //tablas.add(tabla);
        tablas.put(tabla.getNombre(), tabla);
        //tabla.nombreInvariable = true;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("tablas={");
        for(Map.Entry<String,TablaXml> e : tablas.entrySet()) {
            sb.append(e.getValue().getNombre());
            sb.append(", ");
        }
        int n = sb.length();
        sb.delete(n-2,n);
        sb.append("}");
        //return "BD{" + "tablas=" + tablas + '}';
        return sb.toString();
    }

    public TablaXml getTabla(String nombreTabla) {
        return tablas.get(nombreTabla);
    }
    
    private String scriptFromIterarTablas(ScriptTabla st) throws Exception {
        StringBuilder sb = new StringBuilder();        
        for(Map.Entry<String,TablaXml> e : tablas.entrySet()) {
            TablaXml t = e.getValue();
            if(t.tieneRegistros()) {
                st.ejecutar(t,sb);
            }
        }
        return sb.toString();
    }

    public String scriptsCreateTableIfNoExits() throws Exception {
        return scriptFromIterarTablas(new ScriptTabla() {
            @Override
            public void ejecutar(TablaXml t, StringBuilder sb) throws Exception {
                sb.append("create table if not exists "+t.getNombre()+" (");
                sb.append(t.getCamposString());            
                sb.append(");");
            }
        });
        /*
        StringBuilder sb = new StringBuilder();        
        for(Map.Entry<String,Tabla> e : tablas.entrySet()) {
            Tabla t = e.getValue();
            if(t.tieneCampos()) {
                sb.append("create table if not exists "+t.getNombre()+" (");
                sb.append(t.getCamposString());            
                sb.append(");");
            }
        }
        return sb.toString();
        */
    }

    /*
    public String scriptsInsert() throws Exception {
        return scriptFromIterarTablas(new ScriptTabla() {
            @Override
            public void ejecutar(TablaXml t, StringBuilder sb) throws Exception {
                sb.append(t.scriptInsert()); 
            }
        });
    }*/

    public String scriptsDropTableIfExits() throws Exception {
        return scriptFromIterarTablas(new ScriptTabla() {
            @Override
            public void ejecutar(TablaXml t, StringBuilder sb) throws Exception {
                sb.append("drop table if exists "+t.getNombre()+";");
            }
        });
    }
    
    private interface ScriptTabla {
        void ejecutar(TablaXml t, StringBuilder sb) throws Exception;
    }
    
}
